package com.example.demo.service;

import com.example.demo.dao.DemoDao;
import com.example.demo.poji.DtoThree;
import com.example.demo.poji.Dtoaaa;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DemoService {

    @Autowired
    private DemoDao demoDao;

    /**
     * 字典列表
     * @return
     * @throws Exception
     */
    public List<Map> loadDictList(Map param) throws Exception{
        return demoDao.loadDictList(param);
    }


    /**
     * 字典列表
     * @return
     * @throws Exception
     */
    public void savadata(Map param) throws Exception{
       demoDao.insertdata(param);
    }

    //    第一题
    public List<Map> allons() {
        List<Map> listallones = demoDao.listallones();
        return listallones;
    }

//    第二题
    public List<Map> alltwo() {
        List<Map> listalltwo = demoDao.listalltwo();
        return listalltwo;
    }

//    第三题
    public List<Map<String, List<Dtoaaa>>> allthree(){
        List<Map<String, List<Dtoaaa>>> result = new ArrayList<Map<String, List<Dtoaaa>>>();
        List<DtoThree> listallthree = demoDao.listallthrees();
        List<String> collect = listallthree.stream().distinct().map(e -> e.getTjrq()).collect(Collectors.toList());
        collect =  collect.stream().distinct().collect(Collectors.toList());
        for (String tjrq : collect) {
            List<Dtoaaa> lsList = new ArrayList<Dtoaaa>();
            Map<String, List<Dtoaaa>> map = new HashMap<String, List<Dtoaaa>>();
            for (DtoThree dtoThree : listallthree) {
                if (tjrq.equals(dtoThree.getTjrq())) {
                    Dtoaaa dtoThree1 = new Dtoaaa();
                    dtoThree1.setMc(dtoThree.getMc());
                    dtoThree1.setTjnum(dtoThree.getTjnum());
                    dtoThree1.setTjlb(dtoThree.getTjlb());
                    lsList.add(dtoThree1);
                }
            }
            map.put(tjrq, lsList);
            result.add(map);
        }
        return result;
    }

    //    第四题
    public List<DtoThree> allfour(){
        List<DtoThree> result = new ArrayList<DtoThree>();
        List<DtoThree> listallthree = demoDao.listallthrees();
        List<Integer> collect = listallthree.stream().map(e -> e.getTjnum()).collect(Collectors.toList());
        int max = getMaxFromListBySort(collect);
        for (DtoThree dtoThree : listallthree) {
            if (max == dtoThree.getTjnum()) {
                result.add(dtoThree);
            }
        }
        return result;
    }

    public static  int getMaxFromListBySort(List<Integer> list)
    {
        int max = list.get(0);
        for(Integer i :list){
            if (i>max)
                max = i;
        }
        return max;
    }


}
